package com.naihe.anderson.sms.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.Nhsms;
import com.naihe.anderson.sms.util.LinkifyUtil;

public class TalkMessageAdapter extends BaseAdapter {

	Context mContext;
	List<Nhsms> objects;
	Date nowDate;
	
	private Map<Nhsms, Boolean> checkIfMap = new HashMap<Nhsms, Boolean>();
	
	public TalkMessageAdapter(Context context, List<Nhsms> objects) {
		this.mContext = context;
		this.objects = objects;
		nowDate = new Date();
	}
	
	public void resetList(List<Nhsms> tempList) {
		checkIfMap.clear();
		for(Nhsms entityObj : tempList)
		{
			checkIfMap.put(entityObj, false);
		}
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Nhsms nhsms = objects.get(position);
		
		View view = null;
//		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			if(nhsms.getType() == Nhsms.MESSAGE_TYPE_INBOX)
				view = inflater.inflate(R.layout.talkmns_message_receive_layout, null);
			else
				view = inflater.inflate(R.layout.talkmns_message_send_layout, null);
//		} else {
//			view = convertView;
//		}
			
		TextView mns_time = (TextView) view.findViewById(R.id.mns_time);
		mns_time.setText(getDataFormat(nhsms.getDate()));
		TextView mns_body = (TextView) view.findViewById(R.id.mns_body);
		if(NhSmsApp.DEBUG_MODE){
			mns_body.setText(nhsms.getBody() + "【" + nhsms.getThread_id()+ "-" + nhsms.get_id() + "】");
		} else {
			mns_body.setText(nhsms.getBody());
		}
		
		if(nhsms.getStatus() == Nhsms.STATUS_COMPLETE){
			mns_body.setCompoundDrawables(null, null, getDrawable(R.drawable.sms_success), null);
		} else if(nhsms.getStatus() == Nhsms.STATUS_PENDING){
			mns_body.setCompoundDrawables(null, null, getDrawable(R.drawable.sms_sent), null);
		} else if(nhsms.getStatus() == Nhsms.STATUS_FAILED){
			mns_body.setCompoundDrawables(null, null, getDrawable(R.drawable.sms_error), null);
		} else {
			mns_body.setCompoundDrawables(null, null, null, null);
		}
		
		/**
         * Linkify类的addLinks方法，可以为我们添加一个匹配或者多个匹配（多个匹配之间是通过|分隔的），
         * 当我们的内容和规则符合时，系统便会为我们自动调用系统中相应的应用进行处理。
         */
        LinkifyUtil.addLinks(mns_body, LinkifyUtil.WEB_URLS | LinkifyUtil.EMAIL_ADDRESSES | LinkifyUtil.PHONE_NUMBERS);
        
		CheckBox widgetCheckbox = (CheckBox) view.findViewById(R.id.for_del_pic);
		
		nhsms = null;
		return view;
	}

	@Override
	public int getCount() {
		return objects.size();
	}

	@Override
	public Nhsms getItem(int position) {
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public Map<Nhsms, Boolean> getCheckIfMap() {
		return checkIfMap;
	}
	
	public void setCheckIfMap(Nhsms entityObj,boolean values){
		checkIfMap.put(entityObj, values);
	}
	
	public void setCheckAll(boolean checked){
		for(Map.Entry<Nhsms, Boolean> entityMap : checkIfMap.entrySet())   
		{   
			checkIfMap.put(entityMap.getKey(), checked); 
		} 
	}
	
	private Drawable getDrawable(int resourcesId){
		Drawable drawable = mContext.getResources().getDrawable(resourcesId);
		drawable.setBounds(0, 0, 20, 20);
		return drawable;
	}
	
	private String getDataFormat(long time){
        String formatTime = "";
        Date date = new Date(time);
        try {
            if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==nowDate.getDate()){
				formatTime = mContext.getResources().getString(R.string.today) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==(nowDate.getDate()-1)){
				formatTime = mContext.getResources().getString(R.string.yesterday) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear()){
				formatTime = new SimpleDateFormat("MM-dd").format(date);
			}else{
				formatTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			}
        } catch (Exception e) {
            formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);;
        }
        return formatTime;
    }
}
